package com.jonychen.swagger2demo.common.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jonychen.swagger2demo.common.constant.Constant;
import com.jonychen.swagger2demo.common.entity.model.User;
import com.jonychen.swagger2demo.common.enums.CodeEnum;
import com.jonychen.swagger2demo.common.util.WafRequestWrapper;
import com.jonychen.swagger2demo.service.RedisService;
import com.jonychen.swagger2demo.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.MultipartResolver;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description:基础Controller
 * @Author: kui.chen@100credit.com
 * @Date: 2019/9/7 21:33
 */
public class BaseController implements ServletContextAware {

    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    private ServletContext application;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisService redisService;

    @Autowired
    public HttpServletRequest request;

    @Resource
    public HttpServletResponse response;

    @Deprecated
    public HttpSession session;

    @Autowired(required = false)
    protected MultipartResolver multipartResolver;

    public BaseController() {
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.application=servletContext;
    }

    /**
     * 获取包装防Xss Sql注入的 HttpServletRequest
     * @return request
     */
    public void setRequest() {
        this.request=new WafRequestWrapper(request);
    }


    /**
     * @description:从session中获取当前登录用户信息
     */
    protected User getSessionCurrentUser() {
        String sessionId = request.getHeader("sessionId");
        if (sessionId.contains(",")) {
            sessionId = sessionId.split(",")[0];
        }
        String userMsg = redisService.get(sessionId, Constant.USER_SESSION_FLAG);
        if (StringUtils.isBlank(userMsg)) {
            return null;
        }
        return JSON.parseObject(userMsg, User.class);
    }

    /**
     * @description:从数据库中获取当前登录用户信息并存入redis中
     */
    protected User getCurrentUser() {
        Integer userId=getSessionCurrentUser().getUserId();
        String userDetail = redisService.get(String.valueOf(userId),Constant.USER__UERID_FLAG);
        if (StringUtils.isNotBlank(userDetail)) {
            return    JSON.parseObject(userDetail, User.class);
        }
        // 从数据库中重新查询
        User user = userService.selectById(userId);
        redisService.set(String.valueOf(userId), JSON.toJSONString(user), Constant.USER__UERID_FLAG);
        redisService.expire(String.valueOf(userId), Constant.USER__UERID_FLAG, 60 * 60);
        return user;
    }

    public static String respJson(CodeEnum code) {
        return respJson(code, null);
    }

    public static String respJson(String code, String message) {
        return respJson(code, message, null);
    }

    public static String respJson(CodeEnum code, Object data) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code.getCode());
        jsonObject.put("message", code.getMessage());
        if (data != null) {
            jsonObject.put("data", data);
        }
        return jsonObject.toJSONString();
    }


    public static String respJson(String code, String message, Object data) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);
        jsonObject.put("message", message);
        if (data != null) {
            jsonObject.put("data", data);
        }
        return JSON.toJSONString(jsonObject);
    }
}
